package iceberg.rules;

import iceberg.Reference;
import iceberg.constraints.ConstraintException;
import iceberg.constraints.IConstraintResult;
import iceberg.domain.Rule;
import iceberg.domain.StructureDefinition;
import iceberg.readers.XMLReader;

import java.util.ArrayList;
import java.util.List;

public class RuleDispatcher {

	private static final StructureDefinition resolver = new XMLReader().getDefinition();

	public List<IConstraintResult> invokeRules(Reference reference) {
		List<IConstraintResult> results = new ArrayList<IConstraintResult>();
		for(Rule rule : resolver.getRules()) {
			IConstraintResult result = rule.performCheck(reference);
			if(result != null) {
				results.add(result);
			}
		}

		return results;
	}

	public ConstraintException getConstraintException(Reference reference) {
		return resolver.getConstraintException(reference.getFrom().getName());
	}
}
